package com.qbw.app.manager;

import android.text.TextUtils;

import com.qbw.app.model.User;
import com.qbw.spm.P;

/*
 *
 * @author QBW
 * @date 2019/8/15
 */
public class UserManager {

    private static UserManager sInstance;

    private final String KEY_USER_TOKEN = "user_token";
    private User mUser;

    private UserManager() {

    }

    public static UserManager getInstance() {
        if (sInstance == null) {
            synchronized (UserManager.class) {
                if (sInstance == null) {
                    sInstance = new UserManager();
                }
            }
        }
        return sInstance;
    }

    public User getUser() {
        if (mUser == null) {
            mUser = P.getObject(KEY_USER_TOKEN, User.class);
        }
        return mUser;
    }

    public void saveUser(User user) {
        mUser = user;
        P.putObject(KEY_USER_TOKEN, mUser);
    }

    public boolean isLogin() {
        User user = getUser();
        return user != null && !TextUtils.isEmpty(user.getToken());
    }

    public void logout() {
        saveUser(null);
    }
}
